package com.punai.config.sqlitedao;


import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.punai.common.core.text.Convert;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.sqlite.SqliteUtils;
import com.punai.config.constants.PnConfigConstants;
import com.punai.config.sqlitedomain.ConfigEngineerLinkElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class ConfigEngineerClassLinkFeatureRepository {
	private final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	@Autowired
	private SqliteUtils sqliteUtils;

	public ConfigEngineerLinkElement save(ConfigEngineerLinkElement ConfigEngineerLinkElement) {
		String sortSql="select max(SORTING) as SORTING from tab_config_link_eng_class_elem";
		String sql="insert into tab_config_link_eng_class_elem("
				+ "FEATURE_ID,"
				+ "CLASS_ID,"
				+ "SORTING,"
				+ "ID,"
				+ "VERSION,"
				+ "SREACH_KEY,"
				+ "LAST_MODIFY,"
				+ "EDIT_STATE,"
				+ "CREATE_DATE) values(?,?,?,?,?,?,?,?,?)";
		//initSqliteConnection();
		Map unique = sqliteUtils.unique(sortSql);
		ConfigEngineerLinkElement.setEditState(PnConfigConstants.EDITSTATE.SAVESTATE.getType());
		ConfigEngineerLinkElement.setVersion(PnConfigConstants.ENTITY_VERSION);
	
		ConfigEngineerLinkElement.setId(IdUtil.fastUUID());
		ConfigEngineerLinkElement.setSorting(unique.get("SORTING")==null?1: Convert.toInt(unique.get("SORTING").toString())+1);
		Object[] args=new Object[]{
				ConfigEngineerLinkElement.getFeatureId(),
				ConfigEngineerLinkElement.getClassId(),
				ConfigEngineerLinkElement.getSorting(),
				ConfigEngineerLinkElement.getId(),
				ConfigEngineerLinkElement.getVersion(),
				ConfigEngineerLinkElement.toString(),
				simpleDateFormat.format(new Date()),
				ConfigEngineerLinkElement.getEditState(),
				simpleDateFormat.format(new Date())
		};
		int rownumber= sqliteUtils.execute(sql,args);
		  if(rownumber==0) {
			  throw new ServiceException("sqlite.message.addException");
		  }
		return ConfigEngineerLinkElement;
	}
	public ConfigEngineerLinkElement findByFeature(String featureid) throws NumberFormatException, ParseException{
		ConfigEngineerLinkElement ConfigEngineerLinkElement=null;
		String sql="select * from tab_config_link_eng_class_elem where FEATURE_ID='"+featureid+"'";
		//initSqliteConnection();
		Map<String, Object> map = sqliteUtils.unique(sql);
		if(MapUtil.isNotEmpty(map)){
			ConfigEngineerLinkElement=new ConfigEngineerLinkElement();
			ConfigEngineerLinkElement.setId(map.get("ID")==null?null:map.get("ID").toString());
			ConfigEngineerLinkElement.setFeatureId(map.get("FEATURE_ID")==null?null:map.get("FEATURE_ID").toString());
			ConfigEngineerLinkElement.setClassId(map.get("CLASS_ID")==null?null:map.get("CLASS_ID").toString());
			String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
			Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Long.parseLong(string));
			ConfigEngineerLinkElement.setCreateDate(parse);
			string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
			parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Long.parseLong(string));
			ConfigEngineerLinkElement.setLastModify(parse);
			ConfigEngineerLinkElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
			ConfigEngineerLinkElement.setSreach(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
			ConfigEngineerLinkElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
			ConfigEngineerLinkElement.setSorting(map.get("SORTING")==null?null:Convert.toInt(map.get("SORTING").toString()));
		}
		return ConfigEngineerLinkElement;
	}
	public void delete(String id) {
		String sql="delete from tab_config_link_eng_class_elem where id='"+id+"'";
		//initSqliteConnection();
		int rownumber= sqliteUtils.execute(sql);
		  if(rownumber==0) {
			  throw new ServiceException("sqlite.message.addException");
		  }
	}
	public ConfigEngineerLinkElement findByFeatureAndClass(String featureid,String calssId) throws NumberFormatException, ParseException{
		ConfigEngineerLinkElement ConfigEngineerLinkElement=null;
		String sql="select * from tab_config_link_eng_class_elem "
					+ "where FEATURE_ID='"+featureid+"' "
							+ "and CLASS_ID='"+calssId+"'";
		//initSqliteConnection();
		Map<String, Object> map = sqliteUtils.unique(sql);
		if(MapUtil.isNotEmpty(map)){
			ConfigEngineerLinkElement=new ConfigEngineerLinkElement();
			ConfigEngineerLinkElement.setId(map.get("ID")==null?null:map.get("ID").toString());
			ConfigEngineerLinkElement.setFeatureId(map.get("FEATURE_ID")==null?null:map.get("FEATURE_ID").toString());
			ConfigEngineerLinkElement.setClassId(map.get("CLASS_ID")==null?null:map.get("CLASS_ID").toString());
			ConfigEngineerLinkElement.setSorting(map.get("SORTING")==null?null:Convert.toInt(map.get("SORTING").toString()));
			String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
			Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Long.parseLong(string));
			ConfigEngineerLinkElement.setCreateDate(parse);
			string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
			parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Long.parseLong(string));
			ConfigEngineerLinkElement.setLastModify(parse);
			ConfigEngineerLinkElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
			ConfigEngineerLinkElement.setSreach(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
			ConfigEngineerLinkElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
		}
		return ConfigEngineerLinkElement;
	}
	public ConfigEngineerLinkElement update(ConfigEngineerLinkElement ConfigEngineerLinkElement) {
		String sql="update tab_config_link_eng_class_elem set "
				+ "FEATURE_ID=?,CLASS_ID=?,SORTING=?,VERSION=?,SREACH_KEY=?,"
				+ "LAST_MODIFY=?,EDIT_STATE=? where id=?";
		//initSqliteConnection();
		ConfigEngineerLinkElement.setEditState(PnConfigConstants.EDITSTATE.UPDATESTATE.getType());
		ConfigEngineerLinkElement.setVersion(ConfigEngineerLinkElement.getVersion()+1);
		ConfigEngineerLinkElement.setSreach(ConfigEngineerLinkElement.toString());
		Object[] args=new Object[]{
				ConfigEngineerLinkElement.getFeatureId(),
				ConfigEngineerLinkElement.getClassId(),
				ConfigEngineerLinkElement.getSorting(),
				ConfigEngineerLinkElement.getVersion(),
				ConfigEngineerLinkElement.getSreach(),
				simpleDateFormat.format(new Date()),
				ConfigEngineerLinkElement.getEditState(),
				ConfigEngineerLinkElement.getId()
		};
		int rownumber= sqliteUtils.execute(sql,args);
		  if(rownumber==0) {
			  throw new ServiceException("sqlite.message.addException");
		  }
		return ConfigEngineerLinkElement;
	}
	public List<ConfigEngineerLinkElement> findByFeatures(List<String> ids) throws NumberFormatException, ParseException {
		ArrayList<ConfigEngineerLinkElement> arrayList = new ArrayList<ConfigEngineerLinkElement>();
		String sql="select * from tab_config_link_eng_class_elem where FEATURE_ID in ("+ StrUtil.join(",",ids)+")";
		List<Map<String, Object>> list = sqliteUtils.list(sql);
		for (Map<String, Object> map : list) {
			ConfigEngineerLinkElement ConfigEngineerLinkElement=new ConfigEngineerLinkElement();
			ConfigEngineerLinkElement.setId(map.get("ID")==null?null:map.get("ID").toString());
			ConfigEngineerLinkElement.setFeatureId(map.get("FEATURE_ID")==null?null:map.get("FEATURE_ID").toString());
			ConfigEngineerLinkElement.setClassId(map.get("CLASS_ID")==null?null:map.get("CLASS_ID").toString());
			String string =map.get("CREATE_DATE")==null?null: map.get("CREATE_DATE").toString();
			Date parse =string==null?null: string.contains("-")?simpleDateFormat.parse(string):new Date(Long.parseLong(string));
			ConfigEngineerLinkElement.setCreateDate(parse);
			string = map.get("LAST_MODIFY")==null?null:map.get("LAST_MODIFY").toString();
			parse = string==null?null:string.contains("-")?simpleDateFormat.parse(string):new Date(Long.parseLong(string));
			ConfigEngineerLinkElement.setLastModify(parse);
			ConfigEngineerLinkElement.setEditState(map.get("EDIT_STATE")==null?null:Integer.parseInt(map.get("EDIT_STATE").toString()));
			ConfigEngineerLinkElement.setSreach(map.get("SREACH_KEY")==null?null:map.get("SREACH_KEY").toString());
			ConfigEngineerLinkElement.setVersion(map.get("VERSION")==null?null:Convert.toInt(map.get("VERSION").toString()));
			ConfigEngineerLinkElement.setSorting(map.get("SORTING")==null?null:Convert.toInt(map.get("SORTING").toString()));
			arrayList.add(ConfigEngineerLinkElement);
		}
		return arrayList;
	}
}
